home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr09 / vstsrc.zip / TRACK.C < prev    next >
C/C++ Source or Header  |  1995-01-25  |  14KB  |  407 lines

  1. /*
  2.  * %W% %E% %U%  [EXTREL_1.2]
  3.  *
  4.  * VersaTrack orbit calculations are based on those that appear in Dr. Manfred
  5.  * Bester's sattrack program (the Unix(tm) versions 1 and 2).
  6.  *
  7.  * The data from which the maps where generated come from "xsat", an
  8.  * X-Windows program by David A. Curry (N9MSW).
  9.  *
  10.  * Site coordinates come from various sources, including a couple of
  11.  * World Almanacs, and also from both of the programs mentioned above.
  12.  *
  13.  * The following are authors' applicable copyright notices:
  14.  *
  15.  *                                                                               
  16.  * Copyright (c) 1992, 1993, 1994 Manfred Bester. All Rights Reserved.        
  17.  *                                                                           
  18.  * Permission to use, copy, modify, and distribute this software and its      
  19.  * documentation for educational, research and non-profit purposes, without   
  20.  * fee, and without a written agreement is hereby granted, provided that the  
  21.  * above copyright notice and the following three paragraphs appear in all    
  22.  * copies.                                                                    
  23.  *                                                                              
  24.  * Permission to incorporate this software into commercial products may be    
  25.  * obtained from the author, Dr. Manfred Bester, 1636 M. L. King Jr. Way,     
  26.  * Berkeley, CA 94709, USA.                                                   
  27.  *                                                                             
  28.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,  
  29.  * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF    
  30.  * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN ADVISED   
  31.  * OF THE POSSIBILITY OF SUCH DAMAGE.                                         
  32.  *                                                                             
  33.  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT       
  34.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A    
  35.  * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"       
  36.  * BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,  
  37.  * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.                                   
  38.  *                                                                             
  39.  *                                                                             
  40.  * Copyright 1992 by David A. Curry                                            
  41.  *                                                                             
  42.  * Permission to use, copy, modify, distribute, and sell this software and its 
  43.  * documentation for any purpose is hereby granted without fee, provided that  
  44.  * the above copyright notice appear in all copies and that both that copyright
  45.  * notice and this permission notice appear in supporting documentation.  The  
  46.  * author makes no representations about the suitability of this software for  
  47.  * any purpose.  It is provided "as is" without express or implied warranty.   
  48.  *                                                                             
  49.  * David A. Curry, N9MSW                                                       
  50.  * Purdue University                                                           
  51.  * Engineering Computer Network                                                
  52.  * 1285 Electrical Engineering Building                                        
  53.  * West Lafayette, IN 47907                                                    
  54.  * davy@ecn.purdue.edu                                                         
  55.  *                                                                             
  56.  * VersaTrack Copyright (c) 1993, 1994 Siamack Navabpour. All Rights Reserved.
  57.  *
  58.  * Permission is hereby granted to copy, modify and distribute VersaTrack
  59.  * in whole, or in part, for educational, non-profit and non-commercial use
  60.  * only, free of charge or obligation, and without agreement, provided that
  61.  * all copyrights and restrictions noted herein are observed and followed, and
  62.  * additionally, that this and all other copyright notices listed herein
  63.  * appear unaltered in all copies and in all derived work.
  64.  *
  65.  * This notice shall not in any way void or supersede any of the other authors
  66.  * rights or privileges.
  67.  *
  68.  * VersaTrack IS PRESENTED FREE AND "AS IS", WITHOUT ANY WARRANTY OR SUPPORT.
  69.  * YOU USE IT AT YOUR OWN RISK. The author(s) shall not be liable for any
  70.  * direct, indirect, incidental, or consequential damage, loss of profits or
  71.  * other tangible or intangible losses or benefits, arising out of or related
  72.  * to its use. VersaTrack carries no warranty, explicit or implied, including
  73.  * but not limited to those of merchantablity and fitness for a particular
  74.  * purpose.
  75.  *
  76.  * Siamack Navabpour, 12342 Hunter's Chase Dr. Apt. 2114, Austin, TX 78729.
  77.  * sia@bga.com or sia@realtime.com.
  78.  */
  79.  
  80.  
  81. #include <windows.h>
  82. #include <stdio.h>
  83. #include <math.h>
  84. #include <sys/types.h>
  85.  
  86. #include "vstdefs.h"
  87. #include "constant.h"
  88. #include "vsttype.h"
  89. #include "vstextrn.h"
  90.  
  91. static int prevx = -1;
  92. static int prevy = -1;
  93. static double dummya;
  94. extern void convcoord(double, double, int *, int *);
  95.  
  96. static BOOL
  97. DurationTerminate() /* to prevent error caused by compiler optimization */
  98. {
  99.     return duration_stop != 0;
  100. }    
  101.  
  102.  
  103. void
  104. DrawLine(lat, lon)
  105. double lat, lon;
  106. {
  107. #undef ABS
  108. #undef MIN
  109. #undef MAX
  110. #define ABS(x)      (((x) < 0) ? -(x) : (x))
  111. #define MIN(x,y)    (((x) < (y)) ? (x) : (y))
  112. #define MAX(x,y)    (((x) < (y)) ? (y) : (x))
  113.     RECT r;
  114.     int x, y;
  115.  
  116.     convcoord(lat, lon, &x, &y);
  117.     display_lock();
  118.     if (prevx < 0 || (ABS(prevx-x) > 80 || ABS(prevy-y) > 40)) {
  119.         MoveToEx(hCompatDC, x, y, NULL);
  120.         prevx = x;
  121.         prevy = y;
  122.     }
  123.     LineTo(hCompatDC, x, y);
  124.     display_unlock();
  125.     r. left = MIN(x, prevx) -1;
  126.     r. right = MAX(x,prevx) +1;
  127.     r. top = MIN(y, prevy) -1;
  128.     r. bottom = MAX( y, prevy) + 1;
  129.     prevx = x;
  130.     prevy = y;
  131.     RedrawScreen(&r); /* NULL TRUE */
  132. }                            
  133.  
  134. static WINAPI
  135. duration_thread(sp)
  136. select_t *sp;
  137. {
  138.     extern void blankBottom(void);
  139.     double predTime, predStopTime, slowStepTime, fastStepTime;
  140.     double lastTime, tzdiff;
  141.     int lineno;
  142.     BOOL firstLine, was_visible, finishPass;
  143.     track_t *tp;
  144.     result_t *rp;
  145.     satellite_t *satp;
  146.     site_t *sitep;
  147.     char *tzstr;
  148.     SIZE sz;
  149.     int tx=4;
  150.  
  151.     if (!sp)
  152.         return 0;
  153.  
  154.     tp = sp->sl_tp;
  155.     satp = tp->satp;
  156.     sitep = tp->sitep;
  157.     if (!satp || !sitep)
  158.         return 0;
  159.     rp = sp->sl_rp;
  160.  
  161.     gtrFile = NULL;
  162.     if (gtrfilename[0]) {
  163.         gtrFile = fopen(gtrfilename, "w");
  164.         if (gtrFile == NULL) {
  165.             sprintf(tmpbuf,"Unable to open \"%s\" for writing GT data",gtrfilename);
  166.             usermsg(tmpbuf);
  167.         }
  168.     }
  169.  
  170.     firstLine    = FALSE;
  171.     was_visible = 0;
  172.     lineno = 0;
  173.     firstLine = 1;
  174.  
  175.     predTime = tp->starttime;
  176.  
  177.     if (predTime == -1.0)
  178.         predTime = utctime();
  179.  
  180.     lastTime = predTime;
  181.  
  182.     predStopTime = predTime + tp->duration;
  183.  
  184.     if (predStopTime < tp->stoptime)
  185.         predStopTime = tp->stoptime;
  186.  
  187.     predict_init(modelflag, sp, predTime);
  188.  
  189.     if (satp->s_flags & SF_GEOSTAT) {
  190.         prevx = -1;
  191.         sprintf(tmpbuf, "%s is Geostationary. Please use Real-time display.", satp->s_name);
  192.         usermsg(tmpbuf);
  193.         PostMessage(Gwnd, GTR_QUIT, (WPARAM) 0, (LPARAM) sp);
  194.         Sleep(0);
  195.         ExitThread(0);
  196.     }
  197.     prevx = prevy = -1;
  198.  
  199.     slowStepTime = tp->steptime; /* 1.0 / tp->epochmeanmotion / 2000.0; */
  200.     fastStepTime = slowStepTime * 3.0;
  201.  
  202.     MakePen(hCompatDC, PenColor);
  203.     SetROP2(hCompatDC, R2_COPYPEN);
  204.     blankBottom();
  205.  
  206.     sprintf(tmpbuf,"%s Ground Track", satp->s_name);
  207.     DrawTextStr(hCompatDC, tx, tRect.top+2, SRCCOPY, CBLACK, 8, hHelv, tmpbuf, &sz);
  208.     tx += sz.cx+26;
  209.     DrawTextStr(hCompatDC, tx, tRect.top+2, SRCCOPY, CBLACK, 8, hHelv, "Start:", &sz);
  210.     tx += sz.cx+7;
  211.     dateStr(predTime, tp->sitep, sp->flags, 1, tmpbuf);
  212.     DrawTextStr(hCompatDC, tx, tRect.top+2, SRCCOPY, CBLACK, 8, hHelv, tmpbuf, &sz);
  213.     tx += sz.cx+16;
  214.     DrawTextStr(hCompatDC, tx, tRect.top+2, SRCCOPY, CBLACK, 8, hHelv, "End:", &sz);
  215.     tx += sz.cx+7;
  216.     dateStr(predStopTime, tp->sitep, sp->flags, 1, tmpbuf);
  217.     DrawTextStr(hCompatDC, tx, tRect.top+2, SRCCOPY, CBLACK, 8, hHelv, tmpbuf, &sz);
  218.     setwsize(Gwnd, TRUE);
  219.     RedrawScreen(&tRect);
  220.     lineno = 0;
  221.     if (gtrFile) {
  222.         dateStr(predTime, tp->sitep, sp->flags, 1, tmpbuf);
  223.         fprintf(gtrFile, "\nSatellite Name:      %s",satp->s_name);
  224.         if (satp->s_modep && satp->s_modep->alias)
  225.             fprintf(gtrFile," (%s)", satp->s_modep->alias);
  226.         fprintf(gtrFile, "\nElement Set Number:  %d\n",satp->s_elementset);
  227.         fprintf(gtrFile, "Elset Epoch Time:    %s UTC\n",satp->s_epochstr);
  228.         fprintf(gtrFile, "Prediction Start:    %s\n", tmpbuf);
  229.         fprintf(gtrFile, "Duration:            %-.2f days\n", tp->duration);
  230.         fprintf(gtrFile, "Starting Orbit No.:  %d\n", (int) rp->r_orbitnum);
  231.            sprintf(tmpbuf, "(%-.4f %c %-.4f %c)", fabs(sitep->c_lat * CRD),
  232.             sitep->c_lat < 0.0 ? 'S' : 'N', fabs(sitep->c_lng * CRD),
  233.             sitep->c_lng > 0.0 ? 'W' : 'E');
  234.         fprintf(gtrFile, "Ground Station:      %-.31s, %-.39s %s\n",
  235.             sitep->c_name, sitep->c_locale, tmpbuf);
  236.         tzstr = "UTC";
  237.         tzdiff = 0;
  238.         
  239.         if (sp->flags & SE_GTR_LOC) {
  240.             tzstr = timeZoneName;
  241.             tzdiff = timeZone * 24.0;
  242.         }
  243.         else if (sp->flags & SE_GTR_SLT) {
  244.             tzstr = sitep->c_tzname;
  245.             tzdiff = sitep->c_timezone * 24.0;
  246.         }
  247.         fprintf(gtrFile, "Report Time in:      %s (UTC %+2.2lf hrs)\n", tzstr, tzdiff);
  248.     }
  249.     firstLine    = FALSE;
  250.     was_visible =  FALSE;
  251.     lineno = 8;
  252.  
  253.     do {
  254.         if (predict(modelflag, sp, predTime) == 0)
  255.             break;
  256.  
  257.         if (DurationTerminate())
  258.             break;
  259.  
  260.         DrawLine(rp->r_lat, rp->r_long);
  261.  
  262.         if (rp->r_elevation >= minElevation) {
  263.  
  264.             if (++lineno > 55) {
  265.                 firstLine = 1;
  266.                 lineno = 0;
  267.                 if (gtrFile) fprintf(gtrFile,"\f");
  268.             }
  269.             if (gtrFile && ( firstLine || !was_visible)) {
  270.                 if (firstLine && was_visible) {
  271.                     fprintf(gtrFile,"\n\n\n"), lineno += 2;
  272.                 }
  273.                 else {
  274.                     fprintf(gtrFile,"\n\n");
  275.                     printDate(gtrFile, predTime, sitep, sp->flags);
  276.                     fprintf(gtrFile,"  ---  Orbit %ld\n\n",rp->r_orbitnum);
  277.                     if (sp->flags & SE_GTR_LOC)
  278.                         fprintf(gtrFile,"   %-3.3s",timeZoneName);
  279.                     else if (sp->flags & SE_GTR_UTC)
  280.                         fprintf(gtrFile,"   UTC");
  281.                     else
  282.                         fprintf(gtrFile,"   %3s",sitep->c_tzname);
  283.                     fprintf(gtrFile,"   Azimuth  Elev    Range   Lat    Long    Height  Doppler  Loss  Phase InSun\n");
  284.                     fprintf(gtrFile,"          [deg]  [deg]     [km]  [deg]   [deg]    [km]    [kHz]   [dB]\n\n");
  285.                     lineno += 6;
  286.                 }
  287.                 firstLine = 0;
  288.             }
  289.  
  290.             if (gtrFile) {
  291.                 printTime(gtrFile, predTime, sitep, sp->flags);
  292.                 fprintf(gtrFile,"  %5.1lf  %5.1lf  %7.1lf", rp->r_azimuth,
  293.                     rp->r_elevation, rp->r_range);
  294.                 fprintf(gtrFile,"  %+5.1lf  %+6.1lf  %7.1lf", rp->r_lat,
  295.                     rp->r_long, rp->r_height);
  296.                 fprintf(gtrFile,"  %5.llf  %5.1lf  %3d   %c\n", rp->r_doppler,
  297.                     rp->r_pathloss, rp->r_phase, rp->r_insun);
  298.             }
  299.             was_visible = TRUE;
  300.         }
  301.         else {
  302.             was_visible = FALSE;
  303.         }
  304.         predTime   +=  (rp->r_elevation < VLOWELEV) ? fastStepTime : tp->steptime;
  305.  
  306.         finishPass  = (rp->r_elevation >= tp->minelevation) ? TRUE : FALSE;
  307.         
  308.     } while (predTime <= predStopTime || finishPass);
  309.  
  310.     if (DurationTerminate() && gtrFile)
  311.         fprintf(gtrFile,"\n***** TERMINATED BY USER COMMAND *****\n");
  312.         
  313.     prevx = -1;
  314.  
  315.     if (gtrFile)
  316.         fclose(gtrFile);
  317.  
  318.     PostMessage(Gwnd, GTR_QUIT, (WPARAM) 0, (LPARAM) sp);
  319.     Sleep(0);
  320.     Sleep(0);
  321.     ExitThread(0);
  322.     return 0;
  323. }
  324.  
  325. void
  326. duration_predict(sp)
  327. select_t *sp;
  328. {
  329.     long thid;
  330.     extern char *ErrorString(int);
  331.  
  332.     if (!sp || duration_running || duration_stop)
  333.         return;
  334.  
  335.     WaitCursor();
  336.     
  337.     gtr_thread_handle = CreateThread(NULL, 0, (PTHREAD_START_ROUTINE)
  338.         duration_thread, (void *) sp, CREATE_SUSPENDED, &thid);
  339.         
  340.     if ((gtr_thread_handle == NULL) || (gtr_thread_handle ==INVALID_HANDLE_VALUE)) {
  341.         sprintf(tmpbuf,"Cannot create GTR thread: %s",ErrorString(GetLastError()));  
  342.         NormCursor();
  343.         usermsg(tmpbuf);
  344.     }
  345.     else {
  346.         duration_running = 1;
  347.         ResumeThread(gtr_thread_handle);
  348.     }
  349. }
  350.  
  351.  
  352. void
  353. continuous_display_init(sp)
  354. select_t *sp;
  355. {
  356.     extern void rtd_allbegin();
  357.     int r;
  358.  
  359.     if (!selInfo) {
  360.         usermsg("Active List is empty");
  361.         return;
  362.     }
  363.     WaitCursor();
  364.     
  365.     SetROP2(hCompatDC, R2_XORPEN);
  366.     MakePen(hCompatDC, CBLACK);
  367.     MakeBrush(hCompatDC, BrushColor);
  368.  
  369.     if (!sp) {
  370.         for (sp = selInfo; sp; sp = sp->sl_next)
  371.             if ((r =rtd_start(Gwnd, hInst, sp)) == FALSE)
  372.                 break;
  373.     }
  374.     else
  375.         r = rtd_start(Gwnd, hInst, sp);
  376.         
  377.     EnableWindow(Gwnd, TRUE);
  378.     SetFocus(Gwnd);
  379.     
  380.     NormCursor();
  381. }
  382.  
  383. void
  384. continuous_display_finish(sp)
  385. select_t *sp;
  386. {
  387.     select_t *spp;
  388.     int n=0;
  389.  
  390.     if (!selInfo) return;
  391.     
  392.     WaitCursor();
  393.  
  394.     if (!sp) {
  395.         for (spp = selInfo; spp != 0; spp = spp->sl_next)
  396.             n |= rtd_finish(spp);
  397.     }
  398.     else
  399.         n = rtd_finish(sp);
  400.     if (!n) {
  401.         SetFocus(Gwnd);
  402.         NormCursor();
  403.     }
  404. }
  405.  
  406.  
  407.